$(function(){
    var loginName=baseFunction.getUrlParameterValueByKey("loginName");
    var data2 = JSON.parse(JSON.parse(baseFunction.getUserInfo()).user);
    var loginPassword= data2.password; //密码
    var oldPassword=baseFunction.getUrlParameterValueByKey("oldPassword"); //判断是否以前设置了支付密码
    $("#btn").on("click",function(){
        var password1=$("#password1").val();
        var password2=$("#password2").val();
        if (!password1 || !password2) { //判断是否输入了
            $(".prompt").html("请输入密码").show(0).delay(1000).hide(0);
            return;
        }
        if (password1 != password2) { //判断两次密码是否一致
            $(".prompt").html("两次密码输入不一致").show(0).delay(1000).hide(0);
            return;
        }
        // 提高用户体验
        $(".prompt").html("加载中...").show(0);
        if (!oldPassword){ //没设置密码  直接设置密码的情况
            var data = {
                userName: baseFunction.encryption(loginName),
                setType: "1",
                password: baseFunction.encryption(hexMD5(password1)),
            };
        }else{ //之前有设置密码  现在只是修改密码
            var data = {
                userName: baseFunction.encryption(loginName),
                setType: "2",
                modType: "2",
                password: baseFunction.encryption(hexMD5(password1)),
                oldPassword: baseFunction.encryption(hexMD5(oldPassword)),
            };
        }
        baseFunction.netRequest("merchSetPwd",data,function(res){
            
            var message=res.message;
            if(res.code==200){ //设置成功后  重新登录  更新本地数据
                var data={
                    type: "1",
                    userName:baseFunction.encryption(loginName),
                    password:baseFunction.encryption(loginPassword), //登录密码
                }
                baseFunction.netRequest("login",data,function(res){
                    
                    if(res.code==200){
                        $(".prompt").html(message).show(0).delay(1000).hide(0);
                        window.localStorage.setItem("appUserInfo",res.data);
                        setTimeout(function(){
                            window.location.href="my.html?current=3"
                        },1000)
                    }else{ //失败
                        $(".prompt").html(res.message).show(0).delay(2000).hide(0);
                    }
                })
                
            }
        })
    })
})